//
//  ExercisesItemTitleViewController.m
//  ShowYork
//
//  Created by hxh on 2020/9/8.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "ExercisesItemTitleViewController.h"

@interface ExercisesItemTitleViewController ()
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UIView *flagView;
@property (nonatomic,strong)UILabel *subTitleLb;
@property (nonatomic,strong)UILabel *otherTitleLb;
@property (nonatomic,strong)UIImageView *remindImgView;

@property (nonatomic,strong)UIView *coverView;
@property (nonatomic,strong)UIImageView *coverNextImgView;
@property (nonatomic,strong)UIImageView *coverRemoveImgView;
@property (nonatomic,strong)UIImageView *coverPanImgView;


@property (nonatomic,strong)ExercieseQuestionItemModel *currentItemModel;
@property (nonatomic,strong)NSTimer *timer;
@property (nonatomic,assign)CGFloat timeSource;

@end

@implementation ExercisesItemTitleViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    
    self.isHideNavBar = YES;
    
    // Do any additional setup after loading the view.
}

- (void)setUI{
    
    [self.view addSubview:self.titleLb];
    [self.view addSubview:self.flagView];
    [self.view addSubview:self.subTitleLb];
    [self.view addSubview:self.otherTitleLb];
    [self.view addSubview:self.remindImgView];
    
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11,*)) {
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft).offset(32);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight).offset(-32);
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(135);
        }else{
            make.left.equalTo(self.view).offset(32);
            make.right.equalTo(self.view).offset(-32);
            make.top.equalTo(self.view).offset(135);
        }
    }];
    
    [self.flagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLb);
        make.top.equalTo(self.titleLb.mas_bottom).offset(115);
        make.size.mas_offset(CGSizeMake(4, 21));
    }];
    [self.subTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.flagView);
        make.left.equalTo(self.flagView.mas_right).offset(8);
    }];
    
    [self.otherTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.subTitleLb);
        make.top.equalTo(self.subTitleLb.mas_bottom).offset(12);
    }];
    
    [self.remindImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-36);
        make.bottom.equalTo(self.view).offset(-36);
    }];
    self.remindImgView.hidden = !self.isFirst;
}

- (void)setupCoverView{
    self.coverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    self.coverView.backgroundColor = RGBA(0, 0, 0, 0.7);
    
    self.coverRemoveImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_help_tag"]];
    [self.coverView addSubview:self.coverRemoveImgView];
    [self.coverRemoveImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.coverView).offset(-18);
        make.bottom.mas_equalTo(-(230 + GK_SAVEAREA_BTM));
    }];

    self.coverPanImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"exerciese_cover_pan_tag"]];
    [self.coverView addSubview:self.coverPanImgView];
    
    [self.coverPanImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.coverView).offset(-36);
        make.bottom.equalTo(self.coverView).offset(-36);
    }];
    
    
    self.coverNextImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"exerciese_card_cover_tag"]];
    [self.coverView addSubview:self.coverNextImgView];
    [self.coverNextImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.coverPanImgView.mas_left).offset(self.coverPanImgView.cl_width * 0.5);
        make.bottom.equalTo(self.coverPanImgView.mas_top).offset(30);
    }];

    self.coverRemoveImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeHelpView)];
    [self.coverRemoveImgView addGestureRecognizer:tap];
    if (![[[NSUserDefaults standardUserDefaults]objectForKey:@"isFirsrCardTitle"]boolValue]) {
        [[UIApplication sharedApplication].keyWindow addSubview:self.coverView];
    }
}

- (void)removeAllHelpView{
    [self.coverView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.coverView removeFromSuperview];
}

- (void)removeHelpView{
    [self.coverView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.coverView removeFromSuperview];
    [[NSUserDefaults standardUserDefaults]setObject:@1 forKey:@"isFirsrCardTitle"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

- (void)updateQuestionItem:(ExercieseQuestionItemModel *)itemModel atIndex:(NSInteger)index{
    
    if (self.currentItemModel == nil) {
        self.currentItemModel = itemModel;
        self.titleLb.text = itemModel.paperName;
        self.subTitleLb.text = itemModel.subName;
        self.otherTitleLb.text = [NSString stringWithFormat:@"本题共%@小题，共%@分。根据题目要求作答",itemModel.count,itemModel.score];
    }
}

- (void)startTimer{
    if (self.timer == nil){
        self.timeSource = self.currentItemModel.answerTime;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(addAnswerTime) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
}

- (void)addAnswerTime{
    self.timeSource+=0.01;
}

- (void)endTimer{
    self.currentItemModel.answerTime = self.timeSource;
    [self.timer invalidate];
    self.timer = nil;
}

- (UILabel *)titleLb{
    if (!_titleLb) {
        _titleLb = [[UILabel alloc]init];
        _titleLb.textColor = RGBA(35, 24, 21, 1);
        _titleLb.numberOfLines = 0;
        _titleLb.font = [UIFont boldSystemFontOfSize:26];
    }
    return _titleLb;
}
- (UIView *)flagView{
    if (!_flagView) {
        _flagView = [[UIView alloc]init];
        _flagView.backgroundColor = [RPAppManager themColor];
        _flagView.layer.cornerRadius = 2;
        _flagView.layer.masksToBounds = YES;
    }
    return _flagView;
}
- (UILabel *)subTitleLb{
    if (!_subTitleLb) {
        _subTitleLb = [[UILabel alloc]init];
        _subTitleLb.textColor = RGBA(35, 24, 21, 1);
        _subTitleLb.font = [UIFont boldSystemFontOfSize:18];
    }
    return _subTitleLb;
}
- (UILabel *)otherTitleLb{
    if (!_otherTitleLb) {
        _otherTitleLb = [[UILabel alloc]init];
        _otherTitleLb.textColor = RGBA(102, 102, 102, 1);
        _otherTitleLb.font = font(14);
    }
    return _otherTitleLb;
}
- (UIImageView *)remindImgView{
    if (!_remindImgView) {
        _remindImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"exerciese_tag_pan"]];
        _remindImgView.hidden = YES;
    }
    return _remindImgView;
}

@end
